package com.killer.filter;

import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RestfulUrlCleaner {
    private List<RestfulPattern> patterns = new ArrayList<>();

    private RestfulUrlCleaner() {
    }

    /**
     * 根据流量控制规则创建与之匹配的RestfulUrlCleaner
     * @param rules 流量控制规则
     * @return RestfulUrlCleaner
     */
    public static RestfulUrlCleaner create(List<FlowRule> rules) {
        RestfulUrlCleaner cleaner = new RestfulUrlCleaner();
        if (rules == null || rules.size() == 0) {
            return cleaner;
        }
        Pattern p = Pattern.compile("\\{[^\\}]+\\}");
        for (FlowRule rule : rules) {
            Matcher m = p.matcher(rule.getResource());
            //如果发现类似{xxx}的结构，断定其为RESTful接口
            if (m.find()) {
                cleaner.patterns.add(
                        new RestfulPattern(Pattern.compile(m.replaceAll("\\\\S+?")), rule.getResource()));
            }
        }
        //根据正则表达式重新排序
        Collections.sort(cleaner.patterns);
        return cleaner;
    }

    public String clean(String originUrl) {
        for (RestfulPattern pattern : patterns) {
            if (pattern.getPattern().matcher(originUrl).matches()) {
                return pattern.getRealResource();
            }
        }
        return originUrl;
    }
}
